﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MahApps.Metro.Controls;
using MahApps.Metro;
using System.IO.Ports;

namespace wpfMetex
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        static SerialPort myPort;
        public void SerialConnection()
        {
            // Standardwerte setzen
            myPort = new SerialPort();

            myPort.BaudRate = 1200;
            myPort.Parity = Parity.None;
            myPort.StopBits = StopBits.Two;
            myPort.Handshake = Handshake.None;
            myPort.ReadTimeout = 1024;
            myPort.WriteTimeout = 1024;
            myPort.DtrEnable = true;
            myPort.DataBits = 7;
        }

        public void portName(string name)
        {
            // Portname holen
            myPort.PortName = name;
        }

        public void WertAnfordern()
        {
            // Wert anfordern, insofern der Port geöffnet ist. Ansonsten Fehlermeldung ausgeben.
            if (myPort.IsOpen)
            {
                myPort.Write("D");
            }
            else
            {
                Console.WriteLine("Verbindung konnte nicht hergestellt werden.");
            }
        }
        public string WertHolen()
        {
            // Wert holen
            System.Threading.Thread.Sleep(3000);
            byte[] mybuffer;
            mybuffer = new byte[14];
            myPort.Read(mybuffer, 0, 14);

            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            string MyVal = enc.GetString(mybuffer);
            return MyVal;
        }

        public string[] getPortNames()
        {
            // gefundene Portnamen holen
            return SerialPort.GetPortNames();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            btnStart.IsEnabled = false;
            MainWindow Schnittstelle = new MainWindow();  // Serial Connection definieren
            Schnittstelle.SerialConnection();
            string[] portname = Schnittstelle.getPortNames();
            txtbWerte.Text = "Portname: " + portname[0];
            //         Console.WriteLine("Portname: " + portname[0]);
            Schnittstelle.portName(portname[0]);                         // Portnamen festlegen

            myPort.Open();                                          // Port öffnen
            string[] value = new string[6];                         // 6, da sonst exception
            for (int k = 1; k < 6; k++)
            {
                Schnittstelle.WertAnfordern();                      // Port offen?
                value[k] = Schnittstelle.WertHolen();               // k als Wertvariable
                txtbWerte.Text += System.Environment.NewLine + "Wert " + k + ": " + value[k];
                
                //          Console.WriteLine("Wert " + k + ": " + value[k]);   // Wert ausgeben
            }
            myPort.Close();                                         // Port schließen
            btnStart.IsEnabled = true;
            //        Console.WriteLine("Press any key to continue...");      // Pause um Ergebnisse auszuwerten
            //Console.ReadKey();
        }

        private void btnSettings_Click(object sender, RoutedEventArgs e)
        {
            if (gridContent.Visibility == Visibility.Visible)
            {
                gridContent.Visibility = Visibility.Hidden;
                gridSettings.Visibility = Visibility.Visible;
            }
            else
            {
                gridContent.Visibility = Visibility.Visible;
                gridSettings.Visibility = Visibility.Hidden;
            }
        }
    }
}
